package com.niubility.server.Impl;

import com.niubility.dao.UsersDao;
import com.niubility.entity.Users;
import com.niubility.server.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

@Service
public  class UsersService implements IUserService {
    @Resource
    private UsersDao usersDao;

    //根据id查询用户
    public Users LoadById(int userId) {
       return usersDao.LoadById(userId);
    }

    //查询所有用户
    public List<Users> LoadAll(Integer page, Integer dispalyCount) {
        return usersDao.LoadAll(page,dispalyCount);
    }

    //增加用户
    public boolean Save(Users users) {
        int i = usersDao.Save(users);
        return i>0?true:false;
    }

    //根据id删除用户
    public boolean Delete(int id) {
        return usersDao.Delete(id)>0?true:false;
    }

    //删除多个用户信息
    public boolean deleteUserByIds(Integer[] userIds) {
        int i= usersDao.deleteUserByIds(userIds);
        return i>0?true:false;
    }

    //用户信息修改
    public boolean Update(Users users) {
        int i= usersDao.Update(users);
        return i>0?true:false;
    }

    //通过邮箱或电话查询到用户
    public List<Users> selectUserByEmailAndTel(Users user){
        List<Users> list = usersDao.selectUserByEmailAndTel(user);
        return list;
    }

    //根据用户电话查询用户
    public Users selectUserByTel(String userTel) {
        Users user = usersDao.selectUserByTel(userTel);
        return user;
    }

    //根据用户邮箱查询用户
    public Users selectUserByEmail(String userEmail) {
        Users user = usersDao.selectUserByEmail(userEmail);
        return user;
    }

    //找到最后一次插入数据的Id
    public int lastInsertId(){
         return usersDao.lastInsertId();
    }


    //用于分页
    public int LoadCount(){
        return usersDao.LoadCount();
    }

    //返回用户消息是否更新
    public int modifyUserMessageStatus(int userId) {
        return usersDao.modifyUserMessageStatus(userId);
    }

    //正则判断手机号
    public boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }
}
